﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using WmsService.AOP;
using WmsService.Derived;
using WmsService.Exceptions;
using WmsService.Out;
using WmsService.Tacking;

namespace WmsService.BusinessLogic
{
    public interface IStockTackingLogic
    {
        [ValidateTokenCallHandler]
        string GetStockTackingId();

        [ValidateTokenCallHandler]
        StockTackingWrap GetStockTackingWrap(string stockTackingNo);

        [ValidateTokenCallHandler]
        void SaveStockTackingWrap(StockTackingWrap stockTackingWrap);

        [ValidateTokenCallHandler]
        void AffirmStockTacking(string stockTackingNo);

        [ValidateTokenCallHandler]
        List<StockTacking> QueryStockTackingList(string condition);

        [ValidateTokenCallHandler]
        List<ExtendStockTackingDtl> QueryStockTackingDtlList(string condition);
    }

    public class StockTackingLogicImpl : LogicBase,  IStockTackingLogic
    {
       

        public string GetStockTackingId()
        {
            try
            {
                var ctx = Factory.ContextFactory.Create();
                return ctx.ExecuteStoreQuery<String>("select dbo.GetStockTackingNo() as StockTackingNo").FirstOrDefault();
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public StockTackingWrap GetStockTackingWrap(string stockTackingNo)
        {
            try
            {
                var stockTackingWrap = new StockTackingWrap();
                var ctx = Factory.ContextFactory.Create();
                var q = from c in ctx.StockTacking
                        where c.StockTackingNo == stockTackingNo
                        select c;
                var stockTacking = q.FirstOrDefault();
                if (stockTacking == null)
                {
                    Exceptions.ExceptionHandler.ThrowException("不存在盘库单：" + stockTackingNo);
                }
                ctx.Detach(stockTacking);
                stockTackingWrap.PStockTacking = stockTacking;
                var q2 = from c in ctx.StockTackingDtl
                         where c.StockTackingNo == stockTackingNo
                         select c;
                stockTackingWrap.PStockTackingDtl = q2.ToList();
                foreach (var dtl in stockTackingWrap.PStockTackingDtl)
                {
                    ctx.Detach(dtl);
                }
                return stockTackingWrap;
            }
            catch (Exception ex)
            {
                ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public void SaveStockTackingWrap(StockTackingWrap stockTackingWrap)
        {
            try
            {
                var ctx = Factory.ContextFactory.Create();
                if (stockTackingWrap.PStockTacking.WcfEntityState == EntityState.Added)
                {
                    stockTackingWrap.PStockTacking.CreateTime = DateTime.Now;
                    stockTackingWrap.PStockTacking.UpdateTime = DateTime.Now;
                    stockTackingWrap.PStockTacking.CreateUserID= ContextUserID;
                    stockTackingWrap.PStockTacking.CreateUserName = ContextUserName;
                    stockTackingWrap.PStockTacking.UpdateUserID= ContextUserID;
                    stockTackingWrap.PStockTacking.UpdateUserName = ContextUserName;
                }
                else
                {
                    stockTackingWrap.PStockTacking.UpdateTime = DateTime.Now;
                    stockTackingWrap.PStockTacking.UpdateUserID = ContextUserID;
                    stockTackingWrap.PStockTacking.UpdateUserName = ContextUserName;
                }

                ctx.AddToStockTacking(stockTackingWrap.PStockTacking);
                ctx.ObjectStateManager.ChangeObjectState(stockTackingWrap.PStockTacking, stockTackingWrap.PStockTacking.WcfEntityState);
                if (stockTackingWrap.PStockTackingDtl != null && stockTackingWrap.PStockTackingDtl.Count > 0)
                {
                    foreach (var stockTackingDtl in stockTackingWrap.PStockTackingDtl)
                    {
                        if (stockTackingDtl.WcfEntityState == EntityState.Added)
                        {
                            stockTackingDtl.CreateTime = DateTime.Now;
                            stockTackingDtl.UpdateTime = DateTime.Now;
                            stockTackingDtl.CreateUserID = ContextUserID;
                            stockTackingDtl.CreateUserName = ContextUserName;
                            stockTackingDtl.UpdateUserID = ContextUserID;
                            stockTackingDtl.UpdateUserName = ContextUserName;
                            stockTackingDtl.ExtNumeric1 = 0;
                            stockTackingDtl.ExtNumeric2 = 0;
                            stockTackingDtl.ExtNumeric3 = 0;
                            stockTackingDtl.ExtNumeric4 = 0;
                        }
                        else
                        {
                            //将数据库中多个个字段拿出来，赋给要保存的对象，以一致。
                            var temp = from o in ctx.StockTackingDtl
                                       where o.StockTackingDtlNo == stockTackingDtl.StockTackingDtlNo
                                       select o;
                            var stockTackingDtlDb = temp.FirstOrDefault();
                            if(stockTackingDtlDb == null)
                            {
                                ExceptionHandler.ThrowException("明细对象为空，非法操作！");
                            }
                            else
                            {
                                //分离出来，否则stockTackingDtl和他相同的key无法加入ctx
                                ctx.Detach(stockTackingDtlDb);

                                stockTackingDtl.HandleRelateNoteNo = stockTackingDtlDb.HandleRelateNoteNo;
                                stockTackingDtl.HandleResult = stockTackingDtlDb.HandleResult;
                                stockTackingDtl.HandleTime = stockTackingDtlDb.HandleTime;
                                stockTackingDtl.ExtNumeric1 = stockTackingDtlDb.ExtNumeric1;

                            }

                            stockTackingDtl.UpdateTime = DateTime.Now;
                            stockTackingDtl.UpdateUserID = ContextUserID;
                            stockTackingDtl.UpdateUserName = ContextUserName;
                            stockTackingDtl.WcfEntityState = EntityState.Modified;
                        }
                        ctx.AddToStockTackingDtl(stockTackingDtl);
                        ctx.ObjectStateManager.ChangeObjectState(stockTackingDtl, stockTackingDtl.WcfEntityState);
                    }
                }
                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                ExceptionHandler.ThrowException(ex);
            }
        }

        public void AffirmStockTacking(string stockTackingNo)
        {
            throw new NotImplementedException();
        }

        public List<StockTacking> QueryStockTackingList(string condition)
        {
            var ctx = Factory.ContextFactory.Create();
            return ctx.ExecuteStoreQuery<StockTacking>("select * from  dbo.StockTacking where 1=1 "  + condition).ToList();
        }

        //StockTackingCustomNo VARCHAR(36)  ,--盘库单主表自定义单据号码
        //StockTackingTime			datetime  default getdate(),--盘库单日期
        //StockTackingYearMonth		VARCHAR(100)  ,--盘库单年月
        //StockTackingDesc		VARCHAR(100)  ,--盘库说明
        //StockTackingUserID				VARCHAR(20) default '',
        //StockTackingUserName			VARCHAR(20) default '',
        //Remark							VARCHAR(200)  ,--备注

        public List<ExtendStockTackingDtl> QueryStockTackingDtlList(string condition)
        {
            var ctx = Factory.ContextFactory.Create();
            return ctx.ExecuteStoreQuery<ExtendStockTackingDtl>("select * from ( "+
                " select d.*,m.StockTackingCustomNo,m.StockTackingTime,m.StockTackingYearMonth,m.StockTackingDesc,m.StockTackingUserID,m.StockTackingUserName,m.Remark as ParentRemark from  dbo.StockTackingDtl d,dbo.StockTacking m where d.StockTackingNo=m.StockTackingNo "+
                " ) tmp where 1=1 " + condition).ToList();
        }
    }
}